<?php
class MySQL {
	public $link;
	public $result;
	public function __construct($host, $user, $pass){
		$this->link = $this->connect($host, $user, $pass);
	}
	public function __destruct(){
		@mysql_close($link);
		// mysql_free_result($result);		
	}
	public function select_db($name){
		return mysql_select_db($name, $this->link);
	}
	public function query($query){
		echo $query;
		$result = mysql_query($query);
		return $result;
	}
	public function connect($host, $user, $pass){
		return mysql_connect($host, $user, $pass);
	}
	public function ConnectionTest(){
		if (!$this->link) {
		die('Could not connect: ' . mysql_error());
		}
		echo 'Connected successfully';
	}
}
?>
<?php
class DBFToSQL {
	private $dbase_identifier;
	private $TableName;
	private $Database;
	private $FilterArray;
	private $FilterArrayIndex=array();
	private $PrimaryKey;
	public function __construct ($filename){
		$this->dbfopen($filename);
	}
	public function dbfopen($filename){
		$this->dbase_identifier = dbase_open($filename, 0);
	}
	public function dbfclose($dbase_identifier){
		dbase_close($this->dbase_identifier);
	}
	public function SetSQLTableName($name){
		$this->TableName = $name;
	}
	public function Filter($delimiter, $string){
		 $this->FilterArray = explode( $delimiter , $string );
		 // echo var_dump($FilterArray);
	}
	public function FilterFields($Fields){
		$Result = array();
		$i = 0;
		foreach( $Fields as $field )
		{
			foreach( $this->FilterArray as $FilterField )
			{
				if ($FilterField == $field['name']) 
				{
					array_push($Result, $field);
					array_push($this->FilterArrayIndex, $i);
					
				}
			}
			$i++;			
		}
		// echo var_dump($Result);
		// echo var_dump($this->FilterArrayIndex);
		return $Result;
	}
	public function CreateTable(){
		$Fields = $this->FetchDBFFields();
		$Fields = $this->FilterFields($Fields);
		//create query
		$Query = $this->CreateTableQuery($Fields);
		// echo $Query;
		// if ( $this->Query($Query) ) echo "Valami van.";
	}
	public function FetchDBFFields(){
		return dbase_get_header_info($this->dbase_identifier);
	}
	protected function AddPrimaryKey($field){
		if ($field == $this->PrimaryKey) return " PRIMARY KEY";
	}
	public function CreateTableQuery($Fields){
		$query = "DROP TABLE IF EXISTS ".$this->TableName."; ";
		$query .= "CREATE TABLE ".$this->TableName." (";
		$it = 0;
		$field_num = 0;
		foreach($Fields as $field)
		{
			if($x++ != 0)
				$query .= ",";
			switch($field['type'])
			{
				case 'character' : $type = 'CHAR'; $length = $field['length'] > 1 ? "({$field['length']})" : ""; break;
				case 'number' : $type = 'NUMERIC'; $length = "({$field['length']}" . ($field['precision'] > 0 ? ", {$field['precision']})": ")"); break;
		       case '' : $type = ''; break;
			}
			$fields_num++;
			$query .=  "{$field['name']} $type$length".$this->AddPrimaryKey($field['name']);
		}
		// $query .= "PRIMARY KEY";
		$query .= ");";
		// $query .= "COMMIT;";
		// PRIMARY KEY ( `id` ) ) 
		// ENGINE = MYISAM ;";
		return $query;
	}
	public function Query($query){
		$sql = new MySQL("localhost", "szofttech", "alma");
		//$sql->ConnectionTest();
		$sql->select_db("szofttech"); if ( $bool ) echo "db selected";
		$sql->query($query); if ( $boool ) echo gettype($boool);
		return $result;		
	}
	public function SetPrimaryKey($field){
		$this->PrimaryKey = $field;
	}
	public function FetchRecords()
	{
		$this->CreateRecordQuery();
	}
	public function IsFiltered($string)
	{
		foreach ($this->FilterArray as $name)
		{
			// if ( strcmp($name, $string) ) return true;
			if ( $name==$string ) return true;			
		}
		return false;
	}
	public function FetchDBFRecords()
	{
		return @dbase_numrecords($this->dbase_identifier);
	}
	public function DBFNumFields()
	{
		return dbase_numfields ($this->dbase_identifier);
	}
	public function DBFGetRecord($row)
	{
		return dbase_get_record($this->dbase_identifier, $row);
	}
	public function CreateRecordQuery()
	{
		$fields = $this->FetchDBFFields();
		$fields_num = $this->DBFNumFields();
		$records = $this->FetchDBFRecords();
		var_dump($this->FilterArray);
		for($x = 1; $x <= 1/*$records*/; $x++)
		{
			$record = $this->DBFGetRecord($x);
			$query .= "INSERT INTO ".$this->TableName." VALUES (";
			$f = 0;
			foreach($record as $field)
			{
				// if ( $this->IsFiltered( $fields[$f]['name'] ) )				
				{
					echo $f.": ".$fields[$f]['name']."<br>";
					if($f != 0) 
						/*if ( $field =='')*/ $query .= ", ";
					if($field!='' /*&& strcmp(str_repeat(' ', $fields[$f]['length']), $field) != 0*/)
					{
						if($fields[$f]['type'] == 'character') $query .= "'";
						$query .= $f.$field;
						if($fields[$f]['type'] == 'character') $query .= "'";
						
					}
					else
					{
						$query .= "NULL";
					}
				}
				if(++$f >= $fields_num) break;
				
			}
			$query .= ");\r\n";
			echo $query;
		}
	}
}
?>


<?php
$op = new dbftosql("C:\\AppServ\\www\\szofttech_src\\workdir\\ARTIKELC.DBF");
$op->SetSQLTableName("Product");
$op->Filter(",","ARTIKEL_NR,ARTBEZ1,ARTBEZ2,ARTBEZ3,LAENGE_MM,BREITE_MM");
$op->SetPrimaryKey("ARTIKEL_NR");
// $op->SetSQLDatabaseName("Sample");
$op->CreateTable();
$op->FetchRecords();

// $op->CreateDatabase();
// $op->filter($FieldNameArray);
// $op->insertSQL($SQLConnection);
?>